package me.luger.project.fof.service;

import me.luger.core.commom.util.LogUtil;
import me.luger.core.dao.nutz.service.BaseService;
import me.luger.core.mvc.entity.IntCodeResult;
import org.nutz.dao.Dao;
import me.luger.project.fof.entity.FofParamHistoy;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.stereotype.Repository;

@Repository
public class FofParamHistoyService extends BaseService<FofParamHistoy> {
    public FofParamHistoyService(Dao dao) {
        super(dao);
    }

    public IntCodeResult save(Integer fofId, String param){
        FofParamHistoy fofParamHistoy = new FofParamHistoy();
        try{
            fofParamHistoy.setFofId(fofId);
            fofParamHistoy.setParams(param);
            fofParamHistoy.setVer(System.currentTimeMillis());
            fofParamHistoy = this.insert(fofParamHistoy);
            if(null != fofParamHistoy.getId()){
                return IntCodeResult.success("save success").addExt("paramHistoryId",fofParamHistoy.getId());
            }
        }catch (Exception e){
            LogUtil.errorLog(e,"");
        }

        return IntCodeResult.error("save error");
    }

    public String getLatestFofParam(Integer fofId){
        Sql getLatestFofParamSql = Sqls.create("select params from fof_param_histoy where fof_id = @fofId order by ver desc limit 1");
        getLatestFofParamSql.setParam("fofId",fofId);
        FofParamHistoy fofParamHistoy = this.fetchOne(getLatestFofParamSql);
        if(null != fofParamHistoy){
            return fofParamHistoy.getParams();
        }
        return null;
    }
}
